// FUNCTIONS
var menu_control = function(){
    var url = $(this).attr("href");
    location.href=url;
};
            
var sidepane = {};
            
sidepane.menu = {
    control:function(){
        var activeClass = "sp ui-state-active";
        $(".sp.ui-state-active").removeClass("ui-state-active");
        $(this).addClass(activeClass);
        $(this).trigger("mouseout");
    }
    ,
    mouseover:function(){
        $(this).not(".sp.ui-state-active").addClass("sp xmb-selected");
    }
    ,
    mouseout:function(){
        $(this).removeClass("xmb-selected");
    }
};

var login = {};

login.button = {
    
    click : function(){
        
        $("#xmb_login_pane").toggle("","fast",{},function(){
            
            $("#login_box").toggle("slide",{},500)
            
        })
    }
    
}

var textfields = {}

textfields.events = {
    
    foblur : function(){$(this).toggleClass("xmb_txtfld-active");}
    
}

function nodeToString ( node ) {
   var elem = "";
   
   if(typeof(node.append) == "function"){
       $.each(node,function(key,val){
           
           elem+=nodeToString(val);
           
       });
       
       return elem;
   }
   var tmpNode = document.createElement( "div" );
   tmpNode.appendChild( node.cloneNode( true ) );
   var str = tmpNode.innerHTML;
   tmpNode = node = null; // prevent memory leaks in IE
   return str;
}

/** Sample usage of $.dynaContent **/

$(function(){
    
    include.once(
        [
            "sp_extend"
            ,"sp_dyna_content"
            ,"sp_page" 
            ,"sp_ajax_effect"
            ,"sp_boolean_elem"
            ,"sp_dom_post"
            ,"sp_dom_get"
            ,"sp_plugin"
            ,"sp_overload"
            ,"sp_bind_methods"
            ,"sp_object"
            ,"sp_form"
            ,"sp_bind_once"

            ,"jqForm"
            ,"jqBBQ"
            ,"less"
            ,"stringify"
            ,JS("xmb-defaults.js")
            ,"jqCookie"
        ], 
        function(){

            main = {
                
                hashchange : {
                    on_request:function(){
                        con.log("request");
                        $status_bar.status("201");
                    }
                    ,
                    on_hashchange:function(){
                        con.log("hashchange");
                        $status_bar.status("201");
                    }
                    ,
                    success:function(){
                        con.log("success");
                        $status_bar.status("remove");
                        $("button,[type=button],[type=submit]").button()
                    
                        $("[type=text],[type=password]")
                        .addClass("xmb_txtfld")
                        .focus(textfields.events.foblur)
                        .blur(textfields.events.foblur);
                        
                        $("#xmb_main").show();
                    }
                //                ,re_req:true
                }
                
            }
        
            $status_bar = $(".xmb.status-bar");

            $("#xmb_workpane").dyna_content(main.hashchange);

            // Events
            $("#menu-box [name=radio]").click(menu_control);

            // id:$.fn.sp_menu A plugin will be created in the future
            $("#side_pane_box a table.form_menu").click(sidepane.menu.control);
            $("#side_pane_box a table.form_menu").hover(sidepane.menu.mouseover,sidepane.menu.mouseout);
            // !end $.fn.sp_menu

            // temp => helpful to indicate the if some error occured.
            // Login button
            $("#login_man").click(login.button.click);
                       
            
//            $("#temp").extend("dialog");

            $(window).trigger("hashchange");

            
        },null,{

            async:false

        });
    
})

